package com.imc.api.org;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.imc.api.order.pojo.UserOrgAccount;
import com.imc.core.config.feign.MyPageImpl;
import com.imc.core.utils.MysqlUtil;
import com.imc.platform.base.org.pojo.OrgLive;
import com.imc.platform.base.org.service.IOrgService;
import com.imc.platform.base.user.pojo.User;
import com.imc.platform.base.user.service.IUserService;
import com.twp.common.component.process.exception.combo.IComboExecptionForTFunc;
import com.twp.common.tuple.ResultDTO;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.security.Principal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 方法描述:小程序公告接口
 * SJC 2018-9-10 11:26:15
 */
@Slf4j
@Controller
public class OrgApiController {

    @Autowired
    IComboExecptionForTFunc iComboExecptionForTFunc;

    @Autowired
    MysqlUtil mysqlUtil;//数据库操作

    @Autowired
    IOrgService iOrgService;//组织机构服务类

    @Autowired
    IUserService iUserService;//用户服务类

    /**
     * 方法描述:获取学校简介、招生简介、教研团队
     * 学校简介：contentType=schoolSummary；招生简介：contentType=enrolStudents；教研团队：contentType=teacherTeam
     * SJC 2018-9-29 17:41:36
     */
    @RequestMapping("/api/public/org/queryIntroduce")
    @ResponseBody
    public ResultDTO queryIntroduce(@RequestParam(defaultValue = "") String introduceName,@RequestParam String contentType, @PageableDefault Pageable pageable) throws Exception {
        return iComboExecptionForTFunc.executeFlow(
                ResultDTO.builder().build(),
                t -> {
                    t.setData(iOrgService.queryIntroduce(introduceName,contentType,pageable));
                    t.setIsSuccess(true);
                },
                (e, t) -> {
                    t.setIsSuccess(false);
                    t.setMessage(e.getMessage());
                    e.printStackTrace();
                    log.error("error:", e.toString());
                }
        );
    }

    /**
     * 方法描述:获取学校简介、招生简介、教研团队
     * 学校简介：contentType=schoolSummary；招生简介：contentType=enrolStudents；教研团队：contentType=teacherTeam
     * SJC 2018-9-29 17:41:36
     */
    @RequestMapping("/api/public/org/queryIntroduceDetail")
    @ResponseBody
    public ResultDTO queryIntroduceDetail(@RequestParam(defaultValue = "") String introduceId, @RequestParam String contentType) throws Exception {
        return iComboExecptionForTFunc.executeFlow(
                ResultDTO.builder().build(),
                t -> {
                    List <Map <String, Object>> list = iOrgService.queryIntroduceInfo(introduceId, contentType);
                    if (list.size() > 0) {
                        t.setData(list.get(0));
                        t.setIsSuccess(true);
                    } else {
                        t.setIsSuccess(false);
                    }
                },
                (e, t) -> {
                    t.setIsSuccess(false);
                    t.setMessage(e.getMessage());
                    log.error("error:", e.toString());
                }
        );
    }

//    /*
//     * YT  2018/10/28 21:47
//     * 方法描述：查询老师团队
//     */
//    @GetMapping("/api/org/queryTeacherTeam")
//    @ResponseBody
//    ResultDTO queryTeacherTeam() {
//        return iComboExecptionForTFunc.executeFlow(
//                ResultDTO.builder().build(),
//                t -> {
//                    t.setData(iOrgService.queryTeacherTeam());
//                    t.setIsSuccess(true);
//                }, (e, t) -> {
//                    t.setIsSuccess(false);
//                    e.printStackTrace();
//                    log.error("error:", e.toString());
//                });
//    }

    /*
     * YT  2018/10/28 21:48
     * 方法描述：查询老师部门
     */
    @GetMapping("/api/public/org/queryTeacherDet")
    @ResponseBody
    ResultDTO queryTeacherDet(String userId) {
        return iComboExecptionForTFunc.executeFlow(
                ResultDTO.builder().build(),
                t -> {
                    t.setData(iOrgService.queryTeacherDet(userId));
                }, (e, t) -> {
                    t.setIsSuccess(false);
                    e.printStackTrace();
                    log.error("error:", e.toString());
                });
    }

    @GetMapping("/api/org/queryLiveList")
    @ResponseBody
    ResultDTO queryLiveList(Principal principal){
        return iComboExecptionForTFunc.executeFlow(
          ResultDTO.builder().build(),
          t->{
              Boolean flog=true;
              User user=iUserService.queryRoleUserByLoginName(principal.getName());
              PageInfo pageInfo=new PageInfo();
              //管理员跟总校长
              if(user.getDuty().equals("master")){
                  pageInfo=iOrgService.queryLiveAllList(null);
              }else {
                  UserOrgAccount userOrgAccount =iUserService.queryUserOrgAccount(String.valueOf(user.getId()));
                  if(StringUtils.isEmpty(userOrgAccount)){
                      flog=false;
                  }else {
                      pageInfo=iOrgService.queryLiveList(null,String.valueOf(userOrgAccount.getOrgId()));
                  }
              }
              t.setIsSuccess(flog);
              t.setMessage(flog ? "查询成功":"该用户无观看权限,请购买课程");
              t.setCode(flog?200:202);
              t.setData(pageInfo.getList());
          }   ,(e,t)->{
              t.setIsSuccess(false);
              t.setMessage("查询失败");
              t.setCode(201);
              e.printStackTrace();
              log.error("error:",e.getMessage());
                }
        );
    }

    @GetMapping("/api/org/updateNum/{liveId}/{type}/{operate}")
    @ResponseBody
    ResultDTO updateNum(@PathVariable String liveId,@PathVariable String type,@PathVariable String operate){
        return iComboExecptionForTFunc.executeFlow(
          ResultDTO.builder().build(),
          t->{
              Map<String,Object>parms=new HashMap<>();
              parms.put("liveId",liveId);
              parms.put("type",type);
              parms.put("operate",operate);
              int retNum=iOrgService.updateOrgLiveState(parms);
              t.setIsSuccess(retNum>0?true:false);
              t.setMessage(retNum>0?"修改成功":"修改失败");
              t.setCode(retNum>0?200:201);
              t.setData(retNum);
          }   ,(e,t)->{
                    t.setIsSuccess(false);
                    t.setMessage("修改失败");
                    t.setCode(201);
                    e.printStackTrace();
                    log.error(liveId+"修改"+type+"的"+operate+"出现异常:error:",e.getMessage());
                }
        );

    }
}
